import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

// 获取当前文件的目录路径（ES模块中的__dirname替代方案）
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// 生成酷炫的图标文件
// 创建Base64编码的PNG数据，直接写入文件

// 生成简单但酷炫的PNG图标数据（Base64编码）
const createPngIcon = (size, color1 = '#667eea', color2 = '#764ba2') => {
  // 这里我们创建一个简单的HTML canvas来生成PNG
  const html = `
<!DOCTYPE html>
<html>
<head><title>Icon Generator</title></head>
<body>
<canvas id="canvas" width="${size}" height="${size}"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

// 创建渐变背景
const gradient = ctx.createLinearGradient(0, 0, ${size}, ${size});
gradient.addColorStop(0, '${color1}');
gradient.addColorStop(1, '${color2}');

// 绘制圆形背景
ctx.beginPath();
ctx.arc(${size/2}, ${size/2}, ${size/2 - 1}, 0, 2 * Math.PI);
ctx.fillStyle = gradient;
ctx.fill();

// 绘制网络图标
ctx.fillStyle = 'white';
ctx.font = '${Math.floor(size * 0.6)}px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('⚡', ${size/2}, ${size/2});

// 输出base64
console.log(canvas.toDataURL('image/png'));
</script>
</body>
</html>`;
  return html;
};

const createIcon16 = () => {
  // 创建一个简单的16x16 PNG图标数据
  const canvas = `
<svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <linearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="100%">
      <stop offset="0%" style="stop-color:#667eea"/>
      <stop offset="100%" style="stop-color:#764ba2"/>
    </linearGradient>
  </defs>
  <circle cx="8" cy="8" r="7" fill="url(#grad)"/>
  <g fill="white">
    <circle cx="8" cy="8" r="1.5"/>
    <circle cx="4" cy="4" r="0.8"/>
    <circle cx="12" cy="4" r="0.8"/>
    <circle cx="4" cy="12" r="0.8"/>
    <circle cx="12" cy="12" r="0.8"/>
    <line x1="4" y1="4" x2="6.5" y2="6.5" stroke="white" stroke-width="0.6"/>
    <line x1="12" y1="4" x2="9.5" y2="6.5" stroke="white" stroke-width="0.6"/>
    <line x1="4" y1="12" x2="6.5" y2="9.5" stroke="white" stroke-width="0.6"/>
    <line x1="12" y1="12" x2="9.5" y2="9.5" stroke="white" stroke-width="0.6"/>
  </g>
</svg>`;
  return canvas;
};

const createIcon32 = () => {
  return `
<svg width="32" height="32" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <linearGradient id="grad32" x1="0%" y1="0%" x2="100%" y2="100%">
      <stop offset="0%" style="stop-color:#667eea"/>
      <stop offset="50%" style="stop-color:#764ba2"/>
      <stop offset="100%" style="stop-color:#f093fb"/>
    </linearGradient>
  </defs>
  <circle cx="16" cy="16" r="15" fill="url(#grad32)"/>
  <g fill="white">
    <rect x="13" y="13" width="6" height="6" rx="1"/>
    <rect x="14" y="14" width="4" height="4" rx="0.5" fill="#667eea"/>
    <g stroke="white" stroke-width="1.5" fill="none" stroke-linecap="round">
      <path d="M4 12 L12 12"/>
      <path d="M4 16 L12 16"/>
      <path d="M4 20 L12 20"/>
      <path d="M20 12 L28 12"/>
      <path d="M20 16 L28 16"/>
      <path d="M20 20 L28 20"/>
      <path d="M10 12 L12 12 L11 11"/>
      <path d="M20 16 L22 16 L21 15"/>
    </g>
  </g>
</svg>`;
};

const createIcon48 = () => {
  return `
<svg width="48" height="48" viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <linearGradient id="grad48" x1="0%" y1="0%" x2="100%" y2="100%">
      <stop offset="0%" style="stop-color:#667eea"/>
      <stop offset="30%" style="stop-color:#764ba2"/>
      <stop offset="70%" style="stop-color:#f093fb"/>
      <stop offset="100%" style="stop-color:#f5576c"/>
    </linearGradient>
  </defs>
  <circle cx="24" cy="24" r="22" fill="url(#grad48)"/>
  <g fill="white">
    <rect x="18" y="18" width="12" height="12" rx="2"/>
    <rect x="20" y="20" width="8" height="8" rx="1" fill="#667eea"/>
    <circle cx="24" cy="24" r="2"/>
    <g stroke="white" stroke-width="2" fill="none" stroke-linecap="round">
      <path d="M6 18 L16 18"/>
      <path d="M6 24 L16 24"/>
      <path d="M6 30 L16 30"/>
      <path d="M32 18 L42 18"/>
      <path d="M32 24 L42 24"/>
      <path d="M32 30 L42 30"/>
      <path d="M14 18 L16 18 L15 17"/>
      <path d="M32 24 L34 24 L33 23"/>
    </g>
    <circle cx="24" cy="12" r="2" fill="#00ff88"/>
    <circle cx="24" cy="36" r="2" fill="#ff4757"/>
  </g>
</svg>`;
};

// 保存SVG文件到正确的位置
const iconsDir = path.join(__dirname, '../dist/icons');

if (!fs.existsSync(iconsDir)) {
  fs.mkdirSync(iconsDir, { recursive: true });
}

fs.writeFileSync(path.join(iconsDir, 'icon16.svg'), createIcon16());
fs.writeFileSync(path.join(iconsDir, 'icon32.svg'), createIcon32());
fs.writeFileSync(path.join(iconsDir, 'icon48.svg'), createIcon48());

console.log('✅ SVG图标已生成');
console.log('💡 请使用在线SVG转PNG工具将这些SVG转换为PNG格式');
console.log('🔗 推荐工具: https://convertio.co/svg-png/');
